
Alkaline.Modules.newModule('JSDoc', function() {

this.moduleType = Alkaline.MODULE_TYPES.OUTPUT;
this.requiredModules = [{ name: 'Documentation' }];

this.registerSupportedArgument('output', 'o', 1, null);

var TOKEN = Alkaline.JSInfo.TOKEN;

this.run = function(ast) {
	var DocModule = this.compileResult.modules.byName.Documentation;
	this.setup(DocModule);
	this.outputText = this.generateDocumentation(DocModule.root);
	var outFile = this.arguments.output;
	if (outFile)
		writeFile(outFile, this.outputText);
	else
		this.compileResult._msg("No output file specified");
}

this.setup = function(DocModule) {
	this.currentModule = null;
	this.modules = DocModule.modules;
}

this.generateDocumentation = function(root) {
	var str = '/* begin documentation */\n';
	
	str += this.generateForList(root.list);

	str += '\n/* end documentation */';
	return str;
}

this.generateForList = function(list) {
	var str = '';
	var classes = [];
	for (var i = 0; i < list.length; i++)
		if (list[i].isClass())
			classes.push(list[i]);
		else
			str += this.generateFor(list[i]);
	for (var i = 0; i < classes.length; i++)
		str += this.generateFor(classes[i]);
	return str;
}

this.generateFor = function(item) {
	var str = '';
	var path = item.fullName;
	
	if (item.name !== 'prototype' && !item.implied) {
		if (item.module !== this.currentModule) {
			this.currentModule = item.module;
			str += '\n';
			var moduleContent = '@module ' + this.currentModule;
			if (!this.modules[this.currentModule].doneOnce) {
				this.modules[this.currentModule].doneOnce = true;
				moduleContent = this.modules[this.currentModule].description + '\n' + moduleContent;
			}
			str += Alkaline.JSInfo.getCommentStringFromContent(moduleContent);
			str += '\n';
		}
		str += '\n';
		str += item.propertyDescriptor.getFullDocComment();
		str += '\n';
		str += path + ' = something;\n';
	}
	str += this.generateForList(item.list, path);

	return str;
}

});
